/**
 * Copyright 2018 人人开源 https://www.renren.io
 * <p>
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * <p>
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * <p>
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package edu.yzu.testspringboot002.utils.password;

import java.util.Base64;

/**
 * 密码工具类
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0
 */
public class PasswordUtils {
    private static PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

    /**
     * 加密
     * @param str  字符串
     * @return     返回加密字符串
     */
    public static String encode(String str){
        return passwordEncoder.encode(str);
    }


    /**
     * 比较密码是否相等
     * @param str  明文密码
     * @param  password  加密后密码
     * @return     true：成功    false：失败
     */
    public static boolean matches(String str, String password){
    	boolean b = isBase64(str);
		// 对前台传递的密码进行解密
//    	if (b) {
//    		str = decrypt(str);
//    	}
        return passwordEncoder.matches(str, password);
    }

    /**
     * 解密
     * @param str  字符串
     */
    public static String decrypt(String str){

    	//base64解码
		byte [] decoded = Base64.getDecoder().decode(str);
		String decodePassWord = new String(decoded);
		decodePassWord = decodePassWord.substring(3);
		decodePassWord = decodePassWord.substring(0,decodePassWord.indexOf("@yjXc=="));
        return decodePassWord;
    }

    /**
     * 判断一个字符串是否被Base64加密-手机端未加密
     * @param str
     * @return
     */
    private static boolean isBase64(String str) {
        if (str == null || str.trim().length() == 0) {
            return false;
        } else {
            if (str.length() % 4 != 0) {
                return false;
            }

            char[] strChars = str.toCharArray();
            for (char c:strChars) {
                if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')
                    || c == '+' || c == '/' || c == '=') {
                    continue;
                } else {
                    return false;
                }
            }
            return true;
        }
    }

    public static void main(String[] args) {
        String str = "123456";
        String password = encode(str);

        System.out.println(password);
        System.out.println(matches(str, password));

        // 加密
        //String str2 = "123456";
//        byte [] bytes = str2.getBytes();
//        byte [] encode = Base64.getEncoder().encode(bytes);
//        String encodeStr = new String(encode);
//        System.out.println(encodeStr);

        // 解密
//        byte [] decoded = Base64.getDecoder().decode(encodeStr);
//		String decodedStr = new String(decoded);
//		decodedStr = decodedStr.substring(3,decodedStr.length());
//		decodedStr = decodedStr.substring(0,decodedStr.indexOf("@yjXc"));
//		System.out.println(decodedStr);
    }

}
